# -*- coding: utf-8 -*-
#  表单视图
# @Time    ： 2022/1/1  11:41
# @Auther  ： 徐良
# File     :  tableViewBill.py

from PyQt5 import QtWidgets
from PyQt5.QtGui import QBrush, QColor, QPainter
from PyQt5.QtWidgets import QAbstractItemView, QMenu, QTableView, QStyle
from PyQt5.QtCore import Qt, pyqtSignal, QModelIndex
from baseClass.baseTableView import BaseTableView
from css.tableCss import tableViewCss



class TableViewBill(BaseTableView):
    itemDel = pyqtSignal(int)
    itemInsert = pyqtSignal(int)
    itemAppend = pyqtSignal(int)
    indexLast = QModelIndex()

    def __init__(self, parent=None):
        super(TableViewBill, self).__init__(parent)
        # self.setStyleSheet(tableViewCss)
        self.currentRow = 0

        self.setStyleSheet("""
                                    QTableView {
                                        border: 2px solid #1e88e5;
                                        background-color: #f3f3f3;
                                        selection-background-color: #1e88e5;
                                        selection-color: white;
                                    }
                                    QHeaderView::section {
                                        background-color: #1e88e5;
                                        color: white;
                                        padding: 4px;
                                        border: 1px solid #1e88e5;
                                    }
                                """)

        # self.horizontalHeader().setStretchLastSection(True)  # 最后一列决定充满剩下的界面
        self.setSelectionMode(QAbstractItemView.SingleSelection)  # 设置只能选1行
        # self.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置只有行选中

        # 右键功能设置
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.custom_right_menu)


    # 右键功能
    def custom_right_menu(self, pos):
        menu = QMenu()
        opt1 = menu.addAction("删除当前条目")
        menu.addSeparator()
        opt2 = menu.addAction("当前位置插入条目")
        menu.addSeparator()
        opt3 = menu.addAction("添加条目")

        action = menu.exec_(self.mapToGlobal(pos))
        if action == opt1:
            # 删除当前条目
            row = self.currentIndex().row()
            if row == -1:
                row = self.currentRow
            self.itemDel.emit(row)

        elif action == opt2:
            row = self.currentIndex().row()
            if row == -1:
                row = self.currentRow
            self.itemInsert.emit(row)

        elif action == opt3:
            row = self.currentIndex().row()
            if row == -1:
                row = self.currentRow

            self.itemAppend.emit(row)

    def onF7ret(self, dic):
        work = dic['work']
        retFieldList = dic['retFieldList']
        valueList = dic['retValueList']
        index = dic['index']
        model = self.model()
        for field, value in zip(retFieldList, valueList):
            col = work.getColumnByField(field)
            idx = model.index(index.row(), col)
            model.setData(idx, str(value))

        flag = 0
        column = index.column()
        cols = model.columnCount()
        for c in range(column, cols):
            nextIndex = index.sibling(index.row(), c + 1)
            isEdit = work.getBodyIsEdit(c + 1)
            if nextIndex.isValid() and isEdit:
                flag = 1
                self.setCurrentIndex(nextIndex)
                self.edit(nextIndex)
                break

        if flag == 0:
            nextIndex = index.sibling(index.row() + 1, 0)
            if nextIndex.isValid():
                self.setCurrentIndex(nextIndex)
                self.edit(nextIndex)

    def goNextColumn(self, work, index):
        """
        由F7helpEdit控件调用
        :param work:
        :param index:
        :return:
        """
        column = index.column()
        model = self.model()
        cols = model.columnCount()
        for c in range(column, cols):
            nextIndex = index.sibling(index.row(), c + 1)
            isEdit = work.getBodyIsEdit(c + 1)

            if nextIndex.isValid() and isEdit:
                flag = 1
                # self.activateWindow()
                # self.setFocus()
                nextIndex = index.sibling(index.row(), c)  # 因为存在面板本身回车跳转到下一列
                self.setCurrentIndex(nextIndex)
                self.edit(nextIndex)
                break

        if flag == 0:
            nextIndex = index.sibling(index.row() + 1, 0)
            if nextIndex.isValid():
                self.activateWindow()
                self.setFocus()
                self.setCurrentIndex(nextIndex)
                self.edit(nextIndex)
